package com.example.controller;

import com.example.ProcessFlowContext;
import com.example.domain.Product;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class OrderController {

    @Resource
    private FlowExecutor flowExecutor;

    @GetMapping("/case1")
    public ResponseEntity case1() {
        ProcessFlowContext processFlowContext = new ProcessFlowContext();
        processFlowContext.getProductList().add(new Product("办公用品", "商品A", new BigDecimal(100f)));
        processFlowContext.getProductList().add(new Product("家电", "商品B", new BigDecimal(1100f)));

        LiteflowResponse response = flowExecutor.execute2Resp("toc", null, processFlowContext);
        ProcessFlowContext context = response.getContextBean(ProcessFlowContext.class);

        if (response.isSuccess()) {
            return new ResponseEntity(context.getOrder(), HttpStatus.OK);
        } else {
            return new ResponseEntity(response.getCause(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
